package rateexchange.client.ui;

import rateexchange.client.RateExchangeConstants;
import rateexchange.client.mvp.RateDataProvider;
import rateexchange.client.place.RatePlace;
import rateexchange.shared.RateProxy;

import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.cellview.client.SimplePager.Resources;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionModel;
import com.google.gwt.view.client.SingleSelectionModel;

public class RatesTableViewImpl extends Composite implements RatesTableView{
	
	private Presenter				presenter= null;
	private CellTable<RateProxy>	ratetable;
	private SimplePager            	pager;
	private RateDataProvider		dataprovider;
	private Button 					reqBut;
	private Button 					rateBut;
	private RateExchangeConstants constants = GWT.create(RateExchangeConstants.class);
	
	public RatesTableViewImpl(){
		
		
		ratetable = new CellTable<RateProxy>(); 
		ratetable.setPageSize(10);
		ratetable.setRowCount(10);
		
		final SingleSelectionModel<RateProxy> selectionmodel = new SingleSelectionModel<RateProxy>();
		ratetable.setSelectionModel(selectionmodel,DefaultSelectionEventManager.<RateProxy> createCheckboxManager());
		selectionmodel.addSelectionChangeHandler(new SelectionChangeEvent.Handler(){

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				
				 RateProxy selected = selectionmodel.getSelectedObject();
			        if (selected != null) {
			          rateBut.setEnabled(true);
			        }
				
			}
			
			
		});
		initColumns(selectionmodel);
		
		// Paging management 
		pager = new SimplePager(TextLocation.CENTER, (Resources) GWT.create(Resources.class), false, 1000, true);
		pager.setRangeLimited(true);
		pager.setDisplay(ratetable);
		
		reqBut = new Button(constants.request());
		reqBut.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				RatePlace place = new RatePlace(RatePlace.REQ);
				presenter.goTo(place);		
				
			}
		});
		rateBut = new Button(constants.rate());
		rateBut.setEnabled(false);
		rateBut.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				RatePlace place = new RatePlace(RatePlace.RATE);
				place.setRate(selectionmodel.getSelectedObject());
				presenter.goTo(place);
			}
		});
		HorizontalPanel butP = new HorizontalPanel();
		
		VerticalPanel panel = new VerticalPanel();
		
		
		butP.add(reqBut);
		butP.add(rateBut);
		panel.add(ratetable);
		panel.add(pager);
		panel.add(butP);
		initWidget(panel);
		
	}
	
	
	private void initColumns(final SelectionModel<RateProxy> selectionmodel) {
		Column<RateProxy, Boolean> checkColumn = new Column<RateProxy, Boolean>(
		        new CheckboxCell(true, false)) {
		      @Override
		      public Boolean getValue(RateProxy object) {
		        // Get the value from the selection model.
		        return selectionmodel.isSelected(object);
		      }
		    };
		
		TextColumn<RateProxy> idCol = new TextColumn<RateProxy>(){
			@Override
			public String getValue(RateProxy object) {
				return object.getId().toString(); 
			};
		};
		TextColumn<RateProxy> pickupCol = new TextColumn<RateProxy>() {
			@Override
			public String getValue(RateProxy object) {
				return object.getPickup();
				}
		};
		TextColumn<RateProxy> loadingCol = new TextColumn<RateProxy>() {
			@Override
			public String getValue(RateProxy object) {
				return object.getLoading();
			}
		};
		TextColumn<RateProxy> turninCol = new TextColumn<RateProxy>(){
			@Override
			public String getValue(RateProxy object) {			
				return object.getTurnin();
			}
		};
		TextColumn<RateProxy> clientCol = new TextColumn<RateProxy>() {
			@Override
			public String getValue(RateProxy object) {
				return object.getClient();
			}
		};
		TextColumn<RateProxy> serviceCol = new TextColumn<RateProxy>() {
			@Override
			public String getValue(RateProxy object) {
				return object.getService();
			}
		};
		TextColumn<RateProxy> priceCol = new TextColumn<RateProxy>() {
			@Override
			public String getValue(RateProxy object) {
				return object.getPrice();
			}
		};
		
		ratetable.addColumn(checkColumn);
		ratetable.addColumn(idCol,constants.id());
		ratetable.addColumn(pickupCol,constants.pickup());
		ratetable.addColumn(loadingCol,constants.loading());
		ratetable.addColumn(turninCol,constants.turnin());
		ratetable.addColumn(clientCol,constants.client());
		ratetable.addColumn(serviceCol,constants.service());
		ratetable.addColumn(priceCol,constants.price());
	
	
	}


	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void setPresenter(Presenter listener) {
		this.presenter = listener;
		
	}

	@Override
	public void setRowCount(int count) {
		ratetable.setRowCount(count);
		
	}

	@Override
	public void setDataProvider(RateDataProvider dataProvider) {
		this.dataprovider = dataProvider;
		dataprovider.addDataDisplay(ratetable);
		
	}

}
